﻿
#ifndef _LOGIN_LOGIC_H_
#define _LOGIN_LOGIC_H_

#include <unordered_map>
#include "../share/common_singleton.h"
#include "../share/jsonmessage.h"
#include "../share/proxy_head.h"
#include "../share/object.h"
#include "../share/time_module.h"


enum EAccountLoginStatus
{
	ELOGIN_STATUS_NONE = 0,
	ELOGIN_STATUS_INLINE = 1,		// 排队　	
	ELOGIN_STATUS_CHECKACCOUNT_BEGIN = 2, 	// 去game验证账号
	ELOGIN_STATUS_CHECKACCOUNT_END = 3,		// 验证结束, 等待下一步操作
	ELOGIN_STATUS_REGISTER_BEGIN = 3,	// 注册开始
	ELOGIN_STATUS_REGISTER_END = 4,		// 注册结束
	ELOGIN_STATUS_LOGIN_BEGIN = 5,		// 登陆开始
	ELOGIN_STATUS_LOGIN_END	= 6,		// 登陆结束
};

class CLoginAccount : public ::common::CObj
{
public:
	CLoginAccount();
	~CLoginAccount();

	int32_t SetAccountID(int account_id) { account_id_ = account_id; return account_id_; }
	int32_t GetAccountID() { return account_id_; }

	int32_t SetAreaID(int area_id) { area_id_ = area_id; return area_id_; }
	int32_t GetAreaID() { return area_id_; }

	void SetUserName(const std::string& user_name);
	const std::string& GetUserName() { return user_name_; }

	int ChangeLoginStatus(EAccountLoginStatus status);
	int GetLoginStatus() { return login_status_; }

	int GetSocketFD() { return socket_fd_; }
	int SetSocketFD(int fd) { socket_fd_ = fd; return socket_fd_; }

	void SetLoginTime(int32_t ntime) { time_ = ntime; }
	int32_t GetLoginTime() { return time_; }

	virtual int Initialize() { return 0; }
	virtual int Resume() { return 0; }

// 	void SetCreateRoleRequest(PBMsgC2SCreateRoleRequest& request) { create_role_request_.CopyFrom(request); }
// 	PBMsgC2SCreateRoleRequest& GetCreateRoleRequest() { return create_role_request_; }

private:
	int32_t account_id_;
	int32_t area_id_;
	int32_t game_id_;
	std::string	user_name_;
	EAccountLoginStatus login_status_;
	int		socket_fd_;
	int32_t time_;
};


class CTestTimer : public common::CTimer
{
public:
	void TimerActive();
};

typedef std::unordered_map< int, OBJ_ID_TYPE > TObjIDMap;
typedef std::unordered_map< int, int> TSocketObjIDMap;

class CLoginLogic :public common::CSingleton<CLoginLogic>
{
public:
	CLoginLogic(){}
	~CLoginLogic(){}
public:
	void Initialize();
	void TickUpdate();
public:
	// 客户端消息
	static void SGeneralLuaMessageReq(const CJsonMessageBase& msg);
	static void SCheckAccountReq(const CJsonMessageBase& msg);
	static void SPlayerLoginReq(const CJsonMessageBase& msg);
	static void SPlayerRegisterReq(const CJsonMessageBase& msg);

	// 服务器消息
	static void SGeneralLuaMessageReqServer(const common::CProxyHead& head, const CJsonMessageBase& msg);
	static void G2LPlayerRegisterResponse(const common::CProxyHead& head, const CJsonMessageBase& msg);
	static void G2LPlayerCheckAccountResponse(const common::CProxyHead& head, const CJsonMessageBase& msg);
	static void G2LPlayerLoginResponse(const common::CProxyHead& head, const CJsonMessageBase& msg);
	static void G2LGameServerStartNotify(const common::CProxyHead& head, const CJsonMessageBase& msg);
	static void C2LGamePlayerNumReport(const common::CProxyHead& head, const CJsonMessageBase& msg);
	void DoRegisterResponse(int32_t ret, int32_t fd);
	void DoLoginResponse(int32_t ret, uint32_t fd);

	int CheckAccount(int32_t account_id, int32_t fd);
	int AccountLoginBegin(int32_t account_id, int32_t fd);
	void SendCheckAccountRes(int32_t fd, int32_t ret, int32_t account);
	CLoginAccount* GetLoginAccount(int32_t account);
	int32_t CheckConnect(int32_t account_id, int32_t fd);
	int AddLoginAccount(CLoginAccount& account);
	int32_t DespawnClient(int fd);
	void set_game_start() { game_start_ = true; }
	bool game_start() { return game_start_; }
	CLoginAccount* GetLoginAccountByFd(int32_t socketid);
private:
	TObjIDMap account_id_map_;
	TSocketObjIDMap socket_id_map_;
	bool game_start_;

	CTestTimer test_timer_;
};

#endif